package domain

import (
	"AcsGo/internal/util"
	"fmt"
)

// 63001123456789020240623    180329AO|AACSSF00261|AD|CNsfsxbjjhj|AY1AZF4A6|
type ReaderInfoRequest struct {
	Code             util.Field
	Language         util.Field
	Summary          util.Field
	TransactionDate  util.Field
	InstitutionId    util.FieldPrefix
	PatronIdentifier util.FieldPrefix
	PatronPassword   util.FieldPrefix
	Operator         util.FieldPrefix
	SequenceNumber   util.FieldPrefix
}

func HandleReaderInfoRequest(str string) (*ReaderInfoRequest, error) {
	if str == "" {
		return nil, fmt.Errorf("str is empty")
	}
	var query = ReaderInfoRequest{
		Code:             util.Field{Value: "63", Length: 2},
		Language:         util.Field{Value: "001", Length: 3},
		Summary:          util.Field{Value: "1234567890", Length: 10},
		TransactionDate:  util.Field{Value: "", Length: 18},
		InstitutionId:    util.FieldPrefix{Value: "", Prefix: "AO", Suffix: util.PrefixLink},
		PatronIdentifier: util.FieldPrefix{Value: "", Prefix: "AA", Suffix: util.PrefixLink},
		PatronPassword:   util.FieldPrefix{Value: "", Prefix: "AD", Suffix: util.PrefixLink},
		Operator:         util.FieldPrefix{Value: "", Prefix: "CN", Suffix: util.PrefixLink},
		SequenceNumber:   util.FieldPrefix{Value: "", Prefix: "AY", Suffix: util.PrefixLink},
	}

	length := query.Code.Length + query.TransactionDate.Length + query.Language.Length + query.Summary.Length
	if len(str) < length {
		return nil, fmt.Errorf("str is too short")
	}

	strTemp := query.Code.GetValue(str)
	strTemp = query.Language.GetValue(strTemp)
	strTemp = query.Summary.GetValue(strTemp)
	strTemp = query.TransactionDate.GetValue(strTemp)
	strTemp = query.InstitutionId.GetValue(strTemp)
	strTemp = query.PatronIdentifier.GetValue(strTemp)
	strTemp = query.PatronPassword.GetValue(strTemp)
	strTemp = query.Operator.GetValue(strTemp)
	_ = query.SequenceNumber.GetValue(strTemp)
	return &query, nil
}

type ReaderInfoResponse struct {
	Code                util.Field
	PatronStatus        util.Field // 14空格
	Language            util.Field
	TransactionDate     util.Field
	HoldItemCount       util.Field
	OverdueItemCount    util.Field
	ChargedItemCount    util.Field
	FineItemsCount      util.Field
	RecallItemCount     util.Field
	InstitutionId       util.FieldPrefix
	PatronIdentifier    util.FieldPrefix
	PersonalName        util.FieldPrefix
	Fee                 util.FieldPrefix
	ValidPatron         util.FieldPrefix
	ValidPatronPassword util.FieldPrefix
	FeeAmount           util.FieldPrefix
	PrePay              util.FieldPrefix
	FeeJ                util.FieldPrefix
	Depositrate         util.FieldPrefix
	LoadedValue         util.FieldPrefix
	HoldItemsLimit      util.FieldPrefix
	ReaderType          util.FieldPrefix
	EndDate             util.FieldPrefix
	HoldItems           util.FieldPrefix
	ScreenMessage       util.FieldPrefix
	PrintLine           util.FieldPrefix
	SequenceNumber      util.FieldPrefix
}

func (q ReaderInfoResponse) StringFormat() (string, error) {
	sb, err := util.ConcatenateFields(q)
	if err != nil {
		return "", err
	}
	return sb, nil
}

func InitReaderInfoResponse(rdbm, rdname, holdItems string) *ReaderInfoResponse {
	// 0000 0000 0000 0000 0000 0000 AO001|AACSSF00261|AE宋义果
	// |CF-2.1|BLY|CQN|BV50.0|JE0.0|JF2.1|XV50.0|XR0.0|XC0.00|BZ0002|LL0000/0002|GL0000/0002
	// |BR2|XTCSSG|XD2051-02-05|AS|BP13706356121|BM13606356686|XO372522197909285814|RU体委小区
	// |BE|BD|XH1979-09-28 00:00:00.0|RS其它|XF|AF读者欠款额为: 2.1超过本馆欠款可借限额: 0.0! 请先交付!|AG读者欠款额为:
	// 2.1超过本馆欠款可借限额: 0.0! 请先交付!|MC108|SR100|PN|FD|AY4AZ608F
	var queryResponse = ReaderInfoResponse{
		Code:                util.Field{Value: "64", Length: 2},
		PatronStatus:        util.Field{Value: "              ", Length: 14},
		Language:            util.Field{Value: "001", Length: 3},
		TransactionDate:     util.Field{Value: util.GetCurrentTimeInFormat(), Length: 18},
		HoldItemCount:       util.Field{Value: "0000", Length: 4},
		OverdueItemCount:    util.Field{Value: "0000", Length: 4},
		ChargedItemCount:    util.Field{Value: "0000", Length: 4},
		FineItemsCount:      util.Field{Value: "0000", Length: 4},
		RecallItemCount:     util.Field{Value: "00000000", Length: 4},
		InstitutionId:       util.FieldPrefix{Value: "001", Prefix: "AO", Suffix: util.PrefixLink},
		PatronIdentifier:    util.FieldPrefix{Value: "", Prefix: "AA", Suffix: util.PrefixLink},
		PersonalName:        util.FieldPrefix{Value: "", Prefix: "AE", Suffix: util.PrefixLink},
		Fee:                 util.FieldPrefix{Value: "0.0", Prefix: "CF", Suffix: util.PrefixLink},
		ValidPatron:         util.FieldPrefix{Value: "Y", Prefix: "BL", Suffix: util.PrefixLink},
		ValidPatronPassword: util.FieldPrefix{Value: "Y", Prefix: "CQ", Suffix: util.PrefixLink},
		FeeAmount:           util.FieldPrefix{Value: "", Prefix: "VB", Suffix: util.PrefixLink},
		PrePay:              util.FieldPrefix{Value: "0.O", Prefix: "JE", Suffix: util.PrefixLink},
		FeeJ:                util.FieldPrefix{Value: "0.0", Prefix: "JF", Suffix: util.PrefixLink},
		Depositrate:         util.FieldPrefix{Value: "0.0", Prefix: "XR", Suffix: util.PrefixLink},
		LoadedValue:         util.FieldPrefix{Value: "", Prefix: "XC", Suffix: util.PrefixLink},
		HoldItemsLimit:      util.FieldPrefix{Value: "0002", Prefix: "BZ", Suffix: util.PrefixLink},
		ReaderType:          util.FieldPrefix{Value: "", Prefix: "XT", Suffix: util.PrefixLink},
		EndDate:             util.FieldPrefix{Value: "2099-02-05", Prefix: "XD", Suffix: util.PrefixLink},
		HoldItems:           util.FieldPrefix{Value: "", Prefix: "AS", Suffix: util.PrefixLink},
		ScreenMessage:       util.FieldPrefix{Value: "", Prefix: "AF", Suffix: util.PrefixLink},
		PrintLine:           util.FieldPrefix{Value: "", Prefix: "AG", Suffix: util.PrefixLink},
		SequenceNumber:      util.FieldPrefix{Value: "4AZ608F", Prefix: "AY", Suffix: util.PrefixLink},
	}
	if rdname == "" {
		message := fmt.Sprintf("[%s]该卡异常,可能未办理读者卡,请联系图书馆!", rdbm)
		queryResponse.ScreenMessage.Value = message
		queryResponse.PrintLine.Value = message
	}
	queryResponse.PatronIdentifier.Value = rdbm
	queryResponse.PersonalName.Value = rdname
	queryResponse.HoldItems.Value = holdItems
	return &queryResponse
}
